package com.FH.permission.privilege;

import com.FH.util.AccessDB;
import com.FH.util.ResultDTO;
import com.FH.util.warnSrc;
import org.codehaus.jettison.json.JSONException;
import org.codehaus.jettison.json.JSONObject;

import javax.ejb.Stateless;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.UUID;

/**
 * Created by Administrator on 2016/8/4.
 */
@Stateless
public class PrivilegeDAOImpl extends AccessDB implements PrivilegeDAO {


    /**
     * get
     * 查询权限
     * @param search
     * @param page
     * @param size
     * @return
     */
    @Override
    public ResultDTO getPrivilegeInfo(String search, String page, String size)  {
        ResultDTO resultDTO = new ResultDTO();
    try {
        JSONObject json = new JSONObject(search);
        String existName = "0";

        String sqlexit = "select count(2) from privilege where 1=1 ";

        String sql_w ="";//查询条件sql组合

        String sql_page = "";//分页sql组合
        Integer page_ok = Integer.parseInt(page);
        Integer size_ok = Integer.parseInt(page);
        if (page.equals("1")) {
            sql_page = " limit 0," + size + "";
        } else {
            sql_page = " limit " + ((page_ok - 1) * size_ok + 1) + "," + size + "";
        }

        if(json.has("contract_id")){
            sql_w += " AND contract_id = '"+json.getString("contract_id")+"'";
        }
        if(json.has("member_id")){
            sql_w += " AND member_id = '"+json.getString("member_id")+"'";
        }
        if(json.has("privilege_id")){
            sql_w += " AND privilege_id = '"+json.getString("privilege_id")+"'";

        }
        String sql_ok = sqlexit+sql_w ;//组合好的sql语句
        existName = getReportingToOne(sql_ok);

        List<String []> seresult = new ArrayList<String []>();

        List<PrivilegeShowDTO> result = new ArrayList<PrivilegeShowDTO>();

        if(!existName.equals("0")){

            String sql ="select privilege_id,org_role_create,org_role_create_time,org_role_modify," +
                    "org_role_modify_time,privilege_type,privilege_name" +
                    " from privilege  where 1=1"  +sql_w + sql_page;
            seresult = this.getReportingTo(sql);

            //构建返回对象
            for (int i = 0; i < seresult.size(); i++){

                PrivilegeShowDTO sysOperatorShowDTO = new PrivilegeShowDTO();

                sysOperatorShowDTO.setPrivilege_id(seresult.get(i)[1]);
                sysOperatorShowDTO.setOrg_role_create(seresult.get(i)[2]);
                sysOperatorShowDTO.setOrg_role_create_time(seresult.get(i)[3]);
                sysOperatorShowDTO.setOrg_role_modify(seresult.get(i)[4]);
                sysOperatorShowDTO.setOrg_role_modify_time(seresult.get(i)[5]);
                sysOperatorShowDTO.setPrivilege_type(seresult.get(i)[6]);
                sysOperatorShowDTO.setPrivilege_name(seresult.get(i)[7]);

                result.add(sysOperatorShowDTO);

            }
        }
        if(seresult.size()!=0) {
            resultDTO.setValues(result);
            resultDTO.setStatus("90000");
            resultDTO.setDescritpion(warnSrc.get_success);
        }else {
            resultDTO.setStatus("90001");
            resultDTO.setDescritpion(warnSrc.get_fail);
        }

    } catch (JSONException e) {
        e.printStackTrace();
    }
    return resultDTO;
}

    @Override
    public ResultDTO addPrivilegeInfo(PrivilegePullDTO dataDTO) {

        ResultDTO resultDTO = new ResultDTO();//构建返回对象

        String uuid = UUID.randomUUID().toString().trim().replaceAll("-", "");

        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

        String time=df.format(new Date());

        String sql = "";

        sql += "insert into privilege(privilege_id,org_role_create,org_role_create_time," +
                "org_role_modify,org_role_modify_time,privilege_type,privilege_name) values" +
                " ('" + uuid + "','" + dataDTO.getOrg_role_create() + "','" + time + "','" + dataDTO.getOrg_role_modify() + "','" + time + "','" + dataDTO.getPrivilege_type() + "','" + dataDTO.getPrivilege_name() + "')";

        int result = this.addReportingTo(sql);

        // 判断SQl是否执行成功
        if (result != 0) {
            resultDTO.setStatus("90000");
            resultDTO.setDescritpion(warnSrc.add_success);
        } else {
            resultDTO.setStatus("90001");
            resultDTO.setDescritpion(warnSrc.add_fail);
        }
        return resultDTO;
    }

    @Override
    public ResultDTO deletePrivilegeInfo(PrivilegePullDTO dataDTO) {
        //删除之前查询一下该表是否有记录
        String sql = "select count(1) from privilege where privilege_id='" + dataDTO.getPrivilege_id() + "'";

        String existName = getReportingToOne(sql);

        int result = 0;

        if (!existName.equals(0)) {

            String QUERY = "";
            QUERY += "delete from privilege where privilege_id='" + dataDTO.getPrivilege_id() + "'";

            result = this.addReportingTo(QUERY);
        }

        ResultDTO resultDTO = new ResultDTO();
        if (result != 0) {
            resultDTO.setStatus("90000");
            resultDTO.setDescritpion(warnSrc.delete_success);

        } else {
            resultDTO.setStatus("90001");
            resultDTO.setDescritpion(warnSrc.delete_fail);
        }
        return resultDTO;
    }
}
